import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart';
import 'package:fwanandroid/model/api/WanAndroidService.dart';
import 'package:fwanandroid/model/bean/Article.dart';
import 'package:fwanandroid/model/bean/BannerInfo.dart';
import 'package:fwanandroid/ui/views/ArticleItemWidget.dart';
import 'package:fwanandroid/ui/views/FSwiperPagination.dart';

class BlogPage extends StatefulWidget {

  int cid;

  BlogPage({super.key,required this.cid});

  @override
  State<BlogPage> createState() => _BlogPageState();
}

class _BlogPageState extends State<BlogPage> with AutomaticKeepAliveClientMixin{
  late EasyRefreshController _controller;
  List<Article> _articles = [];
  List<BannerInfo> _banners = [];
  int _currentPage = 0;

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController(
        controlFinishLoad: true, controlFinishRefresh: true);
    loadArticles(0);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      body: Container(
        color: Colors.white,
        child: EasyRefresh.builder(
            controller: _controller,
            header: const MaterialHeader(),
            footer: const ClassicFooter(),
            onRefresh: () async {
              loadArticles(0);
            },
            onLoad: () async {
              loadArticles(_currentPage + 1);
            },
            childBuilder: (context, physics) {
              return CustomScrollView(
                physics: physics,
                slivers: [
                  SliverList(
                    delegate: SliverChildBuilderDelegate((context, index) {
                      return ArticleItemWidget(article: _articles[index],);
                    }, childCount: _articles.length),
                  )
                ],
              );
            }),
      ),
    );
  }

  // ---------------- 创建组件

  Widget buildBanner(context) {
    return SliverToBoxAdapter(
      child: SizedBox(
        height: 200,
        child: Swiper(
            itemBuilder: (context, index) {
              return Image.network(fit: BoxFit.fill, _banners[index].imagePath);
            },
            itemCount: _banners.length,
            pagination: FSwiperpagination(banners: _banners),
            loop: true,
            autoplay: true),
      ),
    );
  }

  // ------------- 请求数据

  loadArticles(page) {
    WanAndroidService.getSystemTypeDetail(page,widget.cid).then((value) {
      setState(() {
        _currentPage = page;
        if (page == 0) {
          _articles = value.datas;
        } else {
          _articles.addAll(value.datas);
        }
      });
      _controller.finishRefresh();
      _controller.resetFooter();
      _controller.finishLoad(value.curPage >= value.pageCount
          ? IndicatorResult.noMore
          : IndicatorResult.success);
    });
  }

  @override
  bool get wantKeepAlive => true;
}
